package com.sunyard.utils;

import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 时间格式化工具
 *
 * @author xiaoGang
 * @date 2019年4月13日09:31:11
 */
public final class TimeUtil {
    public static final String DEFAULT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME = "yyyyMMddHHmmss";
    public static final String DATE = "yyyyMMdd";
    public static final String DATE_LINE = "yyyy-MM-dd";
    public static final String TIME = "HHmmss";
    public static final String TIME_LINE = "HH:mm:ss";
    public static final String MMDD = "MMdd";

    /**
     * 格式化时间
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String getDatetime(String pattern, Date date) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static String getNowDatetime(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }

    public static Date toDate(String pattern, String date) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException("格式化时间异常");
        }
    }

    public static Date toDate(String date) {
        return toDate(DATE_TIME, date);
    }

    /**
     * 时间格式 yyyy-MM-dd HH:mm:ss
     *
     * @author xiaog.li
     * @date 2019/5/17 15:58
     */
    public static Date toDefaultDate(String date) {
        return toDate(DEFAULT_DATE_TIME, date);
    }

    /**
     * 获取年月
     *
     * @param month 0是当前月，负数取之前月份，整数取之后月份
     * @return
     */
    public static String getMonth(int month) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, month);
        Date date = calendar.getTime();
        return format.format(date);
    }

    /**
     * 判断是否是当天
     *
     * @param transDatetime
     * @return
     */
    public static boolean isToday(String transDatetime) {
        if (StringUtils.isEmpty(transDatetime)) {
            throw new IllegalArgumentException("时间错误");
        }
        String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
        if (transDatetime.length() > 8) {
            transDatetime = transDatetime.substring(0, 8);
        }
        return today.equals(transDatetime);

    }

    /**
     * 实现处理
     *
     * @param date
     * @return
     */
    public static String toDateFormatNoLine(String date) {
        if (StringUtils.isEmpty(date)) {
            return "";
        }
        return date.replaceAll("-", "").replaceAll("_", "");
    }

    /**
     * 获取当天是星期几
     *
     * @return 返回星期几的阿拉伯数字
     */
    public static String getWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        // 1(星期天) 2（星期一） 3（星期二） 4（星期三） 5（星期四） 6（星期五） 7（星期六）
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        return String.valueOf(week);
    }

    /**
     * 获取当天是星期几
     *
     * @return 返回星期几的阿拉伯数字
     */
    public static String getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //  1（星期一） 2（星期二） 3（星期三） 4（星期四） 5（星期五） 6（星期六） 7(星期天)
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (0 == week) {
            week = 7;
        }
        return String.valueOf(week);
    }

    /**
     * 毫秒转日期格式
     *
     * @param timeMillis
     * @return
     */
    public static String millisToDateStr(long timeMillis) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(timeMillis);
        int year = calendar.get(Calendar.YEAR);

        int month = calendar.get(Calendar.MONTH) + 1;
        String mToMonth = null;
        if (String.valueOf(month).length() == 1) {
            mToMonth = "0" + month;
        } else {
            mToMonth = String.valueOf(month);
        }

        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dToDay = null;
        if (String.valueOf(day).length() == 1) {
            dToDay = "0" + day;
        } else {
            dToDay = String.valueOf(day);
        }

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hToHour = null;
        if (String.valueOf(hour).length() == 1) {
            hToHour = "0" + hour;
        } else {
            hToHour = String.valueOf(hour);
        }

        int minute = calendar.get(Calendar.MINUTE);
        String mToMinute = null;
        if (String.valueOf(minute).length() == 1) {
            mToMinute = "0" + minute;
        } else {
            mToMinute = String.valueOf(minute);
        }

        int second = calendar.get(Calendar.SECOND);
        String sToSecond = null;
        if (String.valueOf(second).length() == 1) {
            sToSecond = "0" + second;
        } else {
            sToSecond = String.valueOf(second);
        }
        return year + "-" + mToMonth + "-" + dToDay + " " + hToHour + ":" + mToMinute + ":" + sToSecond;
    }

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        System.out.println(getDayOfWeek());
    }
}
